<?php

namespace app\common\model\system;

use app\common\model\BaseModel;

/**
 * 虚码套餐配置表
 * Class SystemVirtualPackage
 * @package app\common\model\virtual
 */
class SystemVirtualPackageModel extends BaseModel
{
    //
    protected $name = 'system_virtual_package';

    // 设置字段信息
    protected $schema = [
        'id' => 'int',        //
        'title' => 'string',        // 标题
        'money' => 'float',        // 金额
        'num' => 'int',        // 数量
        'sort' => 'int',        // 排序
        'status' => 'int',        // 状态 1:可用 0禁用
        'create_at' => 'datetime',        //
    ];

    /**
     * 套餐列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function packageList(): array
    {
        return $this->db()->where('status', 1)->order('sort desc, id asc')->field('id,title,money,num')->select();
    }

    /**
     * 获取有效的套餐信息
     * @param int $package_id 套餐编号
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getValidInfo(int $package_id)
    {
        return $this->db()->where('status', 1)->where('id', $package_id)->field('id,title,money,num')->find();
    }
}
